#!/bin/bash

## # 初始化个人配置:
##
## e       编辑 init 文件
## vim      clone vim 至 $HOME/.vim
## nvim     下载neovim到/opt/nvim
## docker   配置Docker仓库
## link     一些链接
## software     一些工具
## xcmd     安装x-cmd

base_url="https://gitee.com/mojiey"

echo_green() {
    echo -n -e "\e[32m$*\e[0m\t\t"
}

_help() {
    awk -F '##' '/^##/{print $2}' ~/.config/main.sh
    echo $0
}

_help2() {
    echo_green "初始化个人配置\n"
    echo '-h'
    echo -e '-e\t\t编辑功能文件'
    echo -e 'nvim\t\t下载neovim到/opt/nvim'
    echo -e 'vim\t\tclone vim 至 $HOME/.vim'
    echo -e "link\t\t一些链接"
}

_link() {
    echo_green 2.vim配置
    echo "git clone ${base_url}/myvim.git"
    echo_green 3.文档源码
    echo "git clone ${base_url}/vitepress.doc.git"
    echo_green 4.dotfiles
    echo "git clone ${base_url}/dotfiles.git"
    echo_green 5.neovim
    echo "https://zbluo-file.s3.bitiful.net/neovim-0.11.2-rhel.tar.gz"

}

clone_vim() {
    if [ -e $HOME/.vim ]; then
        echo '$HOME/.vim 已存在'
    else
        git clone ${base_url}/myvim.git ~/.vim
    fi

}

_cli() {
    local url="https://github.com"
    echo -e "gum \t$url/gum\tshell"
    echo -e "glow \t$url/glow\tmarkdown render ter"
}

_proxy() {
    if [ "$2" = "set" ]; then
        _ip=$(gum input --placeholder "default: 127.0.0.1:7890")
        [ -z $_ip ] && _ip=127.0.0.1
        export http_proxy=http://$_ip:7890
        export https_proxy=http://$_ip:7890
        echo http_proxy=$http_proxy
        echo https_proxy=$https_proxy
    elif [ "$2" = "unset" ]; then
        unset http_proxy
        unset https_proxy
    else
        echo http_proxy=$http_proxy
        echo https_proxy=$https_proxy
    fi

}

__docker() {
    tee /etc/yum.repos.d/docker-ce.repo <<EOF
[docker-ce]
name=Docker CE USTC
#baseurl=http://mirrors.ustc.edu.cn/docker-ce/linux/rhel/8/x86_64/stable/
baseurl=http://mirrors.ustc.edu.cn/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck=0
EOF

    echo -e "\nsucssful"
}

download_nvim() {
    if [ -e /opt/nvim.tar.gz ]; then
        echo '/opt/nvim.tar.gz 已存在'
    else
        # wget https://gitee.com/mojiey/neovim/releases/download/v0.10/neovim.tar.gz -O /opt/neovim.tar.gz
        wget -nc "https://zbluo-file.s3.bitiful.net/neovim-0.11.2-rhel.tar.gz" -O /opt/nvim.tar.gz
        [ ! -e /opt/nvim ] && tar xf /opt/nvim.tar.gz -C /opt
    fi

}

_software() {
    echo -e "bash -- x env use chsrc \t\t换源工具"

}

install_xcmd() {
    eval "$(curl https://get.x-cmd.com)"
}

d() {
    case $1 in
    '')

        _help | bat -l sh --theme "Solarized (light)"
        ;;
    'i')
        _i $2
        ;;
    vim)
        clone_vim
        ;;
    links)
        _link
        ;;
    e)
        $EDITOR $HOME/dotfiles/main.sh
        ;;
    nvim)
        download_nvim
        ;;
    '-h')
        echo help
        ;;
    path)
        echo "${PATH//:/\\n}"
        ;;
    proxy)
        _proxy $*
        ;;
    docker)
        __docker
        ;;
    ls | list)
        _software
        ;;
    cli)
        _cli | bat -l sh -p
        ;;
    xcmd)
        install_xcmd
        ;;

    *)
        echo -e "\e[31m$1\e[0m 无效参数"
        ;;
    esac

}
